package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.applozic.mobicomkit.contact.AppContactService;
import com.bumptech.glide.Glide;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.response.DaShangStatResponse;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;
import glideHelp.GlideHelper;

/**
 * Created by ASUS on 2017/11/1.
 */

public class PrizeRedPacketAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<DaShangStatResponse.ListBean> getRedPacketLogResponses;
    private AppContactService appContactService;

    public PrizeRedPacketAdapter(Context context, ArrayList<DaShangStatResponse.ListBean> daShangStatResponse) {
        mContext = context;
        this.getRedPacketLogResponses = daShangStatResponse;
        appContactService = new AppContactService(context);
    }

    @Override
    public int getCount() {
        if (getRedPacketLogResponses == null) {
            return 0;
        }
        return getRedPacketLogResponses.size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.prize_packet_child_item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        holder.time.setText(getRedPacketLogResponses.get(i).getSubmit_time());
        if(getRedPacketLogResponses.get(i).getType3().equals("打赏")){
            holder.money.setText( getRedPacketLogResponses.get(i).getAmount());
            holder.initiative.setVisibility(View.VISIBLE);
            holder.passive.setVisibility(View.GONE);
            holder.initiativeName.setText(appContactService.getContactById(getRedPacketLogResponses.get(i).getTo_nickname()).getDisplayName());
            setPhoto(holder.initiativeAvatar,i, holder);
        } else if(getRedPacketLogResponses.get(i).getType3().equals("被打赏")) {
            holder.money.setText("+" + getRedPacketLogResponses.get(i).getAmount());
            holder.passive.setVisibility(View.VISIBLE);
            holder.initiative.setVisibility(View.GONE);
            holder.passiveName.setText(appContactService.getContactById(getRedPacketLogResponses.get(i).getTo_nickname()).getDisplayName());
            setPhoto(holder.passiveAvatar,i, holder);
        }
        if(getRedPacketLogResponses.get(i).getDescp() == null){
            holder.totalDescp.setVisibility(View.GONE);
        } else {
            holder.totalDescp.setVisibility(View.VISIBLE);
            holder.totalDescp.setText(getRedPacketLogResponses.get(i).getDescp());
        }

        return view;
    }

    private void setPhoto(CircleImageView circleImageView,int i, ViewHolder holder) {
        Glide.with(mContext).load(getRedPacketLogResponses.get(i).getTo_avatar())
                .placeholder(R.mipmap.chat_default)
                .fallback(R.mipmap.chat_default)
                .into(circleImageView);
    }

    static class ViewHolder {
        @BindView(R.id.total_descp)
        TextView totalDescp;
        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.initiative_avatar)
        CircleImageView initiativeAvatar;
        @BindView(R.id.initiative_name)
        TextView initiativeName;
        @BindView(R.id.initiative)
        LinearLayout initiative;
        @BindView(R.id.passive_avatar)
        CircleImageView passiveAvatar;
        @BindView(R.id.passive_name)
        TextView passiveName;
        @BindView(R.id.passive)
        LinearLayout passive;
        @BindView(R.id.money)
        TextView money;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
